package org.ponly.uno4j.support;

import org.ponly.uno4j.core.UnoClient;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * 一个简单的基于 BlockingQueue 队列实现的转换服务
 *
 * @author vacoor
 */
public class SimpleD2sServiceImpl extends D2sService {
    private BlockingQueue<D2sTask> queue = new LinkedBlockingQueue<D2sTask>();

    public SimpleD2sServiceImpl(UnoClient unoClient, int maxWorks) {
        super(unoClient, maxWorks);
    }

    @Override
    protected void addTask(D2sTask task) {
        queue.offer(task);
    }

    @Override
    protected D2sTask acquireTask() {
        try {
            // queue.take();
            return queue.poll(5, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            // throw new IllegalStateException(e);
            return null;
        }
    }
}
